import React, { Component } from 'react';
import axios from 'axios';

class Captcha extends Component {
    state = {}
    render() {
        let w = this.props.w || 120;
        let h = this.props.h || 40;
        return (
            <>
                <img src={this.state.img} alt="点击刷新验证码" onClick={this.loadCaptcha.bind(this)} width={w} height={h}/>
            </>
        );
    }
    componentDidMount(){
        this.loadCaptcha()
    }
    loadCaptcha(){
        axios.get("https://reactapi.iynn.cn/captcha/api/math")
            .then(res => {
                this.setState({
                    key: res.data.key,
                    img: res.data.img
                })
                this.props.setKey(res.data.key)
            })
    }
}

export default Captcha;
